package com.bytz.config;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.autoconfigure.jackson.JacksonProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

@Configuration
public class JacksonConfig {

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(JacksonProperties properties) {
        return builder ->
                builder.postConfigurer(objectMapper ->
                        objectMapper.configOverride(LocalDateTime.class)
                                .setFormat(JsonFormat.Value.forPattern(properties.getDateFormat()))
                );
    }

    @Bean
    public Module javaTimeModule() {
        return new JavaTimeModule();

    }

    @Bean
    public Module jdk8Module() {
        return new Jdk8Module();
    }

}